package common.files.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.util.Iterator;
import java.util.LinkedHashSet;

import common.files.flatfile.FlatFileWriter;
import common.files.xml.xsd.ElementXsd;
import common.files.xml.xsd.TreeNode;

public class XmlHelper {
	private String filename;
	private Document document;
	private ElementXsd root;

	public XmlHelper() {
		root = new ElementXsd();
	}

	public XmlHelper(String filename) throws DocumentException {
		this();
		this.filename = filename;
		this.parse();
	}

	public void parse() throws DocumentException {
		SAXReader reader = new SAXReader();
		this.document = reader.read(new File(filename));
	}

	public void parse(String filename) throws DocumentException {
		this.filename = filename;
		this.parse();
	}

	public Element getRootElement() {
		return this.document.getRootElement();
	}

	public void getGeneralStructure(Element element, ElementXsd parent) {

		for (Iterator<?> ite = element.elementIterator(); ite.hasNext();) {
			Element child = (Element) ite.next();
			System.out.println(child.getQualifiedName());
			ElementXsd exsd = new ElementXsd();
			exsd.setName(child.getQualifiedName());
			exsd.setParent(parent);
			if (child.elementIterator().hasNext()) {
				getGeneralStructure(child, exsd);
			} else {
				// do something....

			}
			if (this.getRootElement().getQualifiedName().equalsIgnoreCase(
					parent.getName())) {
				System.out.println("=============>");

			}
			if (!parent.existChild(exsd)) {
				exsd.getParent().addChild(exsd);
			} else if (parent.hasUpdatedChild(exsd)) {
				exsd.getParent().updateChild(exsd);
			}
		}
	}

	public void getGeneralStructure() {
		root.setName(this.getRootElement().getQualifiedName());
		getGeneralStructure(this.getRootElement(), root);
	}

	public void printStructure(String dest) throws IOException {
		FlatFileWriter ffw = new FlatFileWriter(dest);
		ffw.writePrintln(this.root.toString());
		ffw.commit();
	}

	@Deprecated
	public TreeNode getTreeNode() {
		Element root = this.getRootElement();
		TreeNode treeNode = new TreeNode(root.getQualifiedName());
		this.getTreeNode(root);
		return treeNode;
	}

	@Deprecated
	public void getTreeNode(Element element) {
		LinkedHashSet<String> nodeName = new LinkedHashSet<String>();
		Iterator<?> ite = element.elementIterator();
		Element child;
		while (ite.hasNext()) {
			child = (Element) ite.next();
			nodeName.add(child.getQualifiedName());
		}

		System.out.println(nodeName.size());

	}

	public void unifyInstanceRootName() throws IOException {
		Iterator<Element> ite = this.getRootElement().elementIterator();
		Element element;
		while (ite.hasNext()) {
			element = ite.next();
			System.out.println(element.getQualifiedName() + "\t"
					+ element.getQName());
			element.setQName(new QName("Instance"));
		}
		XMLWriter output = new XMLWriter(new FileWriter(new File("resources\\unifiedInstance.xml")));
		output.write(document);
		output.close();

	}

}
